package com.owen.springboot.smartparkingms.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.owen.springboot.smartparkingms.common.constant.SystemConstant;
import com.owen.springboot.smartparkingms.common.model.Result;
import com.owen.springboot.smartparkingms.module.sys.entity.Config;
import com.owen.springboot.smartparkingms.module.sys.entity.Interface;
import com.owen.springboot.smartparkingms.module.sys.mapper.InterfaceMapper;
import com.owen.springboot.smartparkingms.module.sys.service.IInterfaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Owen
 * @since 2022-02-28
 */
@Service
public class InterfaceServiceImpl extends ServiceImpl<InterfaceMapper, Interface> implements IInterfaceService {

    @Resource
    InterfaceMapper interfaceMapper;

    @Override
    public Result listPage(Interface interf) {
        IPage<Interface> page = new Page(interf.getPageNo(),interf.getPageSize());
        QueryWrapper query = new QueryWrapper<>()
                .like("description",interf.getDescription())
                .or().like("type",interf.getDescription());
        IPage<Config> list = interfaceMapper.selectPage(page,query);
        return Result.ok(list);
    }

    @Override
    public Interface getByType(String type) {
        QueryWrapper queryWrapper = new QueryWrapper<>()
                .eq("type", type);
        return interfaceMapper.selectOne(queryWrapper);
    }

    @Override
    public Result selectquery(Interface entity) {
        Interface interf = getByType(entity.getType());
        if(interf!=null){
            if(interf.getStatus().shortValue() == SystemConstant.StatusType.ENABLE.getValue()){
                List<Map<String,Object>> list = interfaceMapper.selectquery(interf.getType());
                return Result.ok(list);
            }else{
                return Result.error("接口已被禁用");
            }
        }else{
            return Result.error("接口类型不存在");
        }
    }

}
